package com.mix;

import com.aspose.cells.*;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;

public class AsposeCellUtil {

    public static void main(String[] args) throws Exception {
        putWatermark();
    }

    // 比较Workbook的差异，给差异的地方进行标记
    public static void compareWorkbook() throws Exception {
        // Load workbooks
        Workbook workbook1 = new Workbook("C:\\Users\\zhoubin\\Desktop\\代码集合\\file-util\\test-files\\比较两个Excel文件的差异\\file1.xlsx");
        Workbook workbook2 = new Workbook("C:\\Users\\zhoubin\\Desktop\\代码集合\\file-util\\test-files\\比较两个Excel文件的差异\\file2.xlsx");

        // Get first worksheet
        Worksheet sheet1 = workbook1.getWorksheets().get(0);
        Worksheet sheet2 = workbook2.getWorksheets().get(0);

        // Get cells collection
        Cells cells1 = sheet1.getCells();
        Cells cells2 = sheet2.getCells();

        // Compare cells
        for (int row = 0; row <= cells1.getMaxRow(); row++) {
            for (int col = 0; col <= cells1.getRows().get(row).getLastCell().getColumn(); col++) {
                Cell cell1 = cells1.get(row, col);
                Cell cell2 = cells2.get(row, col);
                if (cell1 != null && cell2 != null) {
                    System.out.println(cell1.getValue());
                    System.out.println(cell2.getValue());
                }

                if (cell1 != null && cell2 != null && cell1.getValue() != null && !cell1.getValue().equals(cell2.getValue())) {
                    System.out.println(1);
                    Style style = cell2.getStyle();
                    style.getFont().setBold(true);
                    // Setting the foreground color to yellow
                    style.setForegroundColor(Color.getYellow());

                    // Setting the background pattern to vertical stripe
                    style.setPattern(BackgroundType.SOLID);
                    cell2.setStyle(style);
                    System.out.println(2);
                }
            }
        }

        // Save the second workbook
        workbook2.save("C:\\Users\\zhoubin\\Desktop\\代码集合\\file-util\\test-files\\比较两个Excel文件的差异\\result.xlsx");
    }

    // 给Workbook加上水印
    public static byte[] putWatermark() throws Exception {
        // 先获取文件流 （这边先随意读取文件流）
        InputStream in = new FileInputStream("C:\\Users\\zhoubin\\Desktop\\代码集合\\file-util\\test-files\\比较两个Excel文件的差异\\result.xlsx");
        // 注意document包，每种类型都有一个document
        Workbook workbook = new Workbook(in);
        for (Object worksheet : workbook.getWorksheets()) {
            Worksheet sheet = (Worksheet) worksheet;
            int coloums = sheet.getCells().getColumns().getCount();
            int rows = sheet.getCells().getRows().getCount();
            com.aspose.cells.Shape wordart = sheet.getShapes().addTextEffect(MsoPresetTextEffect.TEXT_EFFECT_1, "水印内容",
                    "宋体", (int) 24f, true, false,
                    rows, rows / 2, coloums / 2, 0, 100, 800);
            MsoFillFormat wordArtFormat = wordart.getFillFormat();
            wordArtFormat.setTransparency(0.9);

            int r = new java.awt.Color(255,0,0).getRed();
            int g = new java.awt.Color(0,255,0).getRed();
            int b = new java.awt.Color(0,0,255).getRed();
            wordArtFormat.setForeColor(com.aspose.cells.Color.fromArgb(r, g, b));
            wordart.setHasLine(false);
            wordart.setLocked(true);
            wordart.setLockedProperty(ShapeLockType.SELECTION, true);
            wordart.setLockedProperty(ShapeLockType.SHAPE_TYPE, true);
            wordart.setLockedProperty(ShapeLockType.MOVE, true);
            wordart.setLockedProperty(ShapeLockType.RESIZE, true);
            wordart.setLockedProperty(ShapeLockType.TEXT, true);
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        workbook.save(out, com.aspose.cells.SaveFormat.XLSX);
        workbook.save("C:\\Users\\zhoubin\\Desktop\\代码集合\\file-util\\test-files\\比较两个Excel文件的差异\\watermark.xlsx");
        out.close();
        return out.toByteArray();
    }
}